json data to html
template literal
map()
ternary operator
arrow function
document.getElementById("app").innerHTML = ``
//back tick tell javascript template literal is created
// eg. `two plus two is ${2 + 2}`
// expression is included
`
hello
hello again
`
`
Pets (${petsData.length} results)
${petsData.toString()}
${petsData.join("-")}
${petsData.map(function(pet){
return pet.name
}).join("")}
${petsData.map(function(pet){
return `
${pet.name} ${pet.species}
`
}).join("")}
// change to function to help reading
function petTemplate(pet) {
return `
${pet.name} ${pet.species}
Age: ${age(pet.birthYear)}
${condition ? yay : nay} //this is ternary operator
${pet.favFoods ? foods(pet.favFoods) : ""}
`
}
function foods (foods) {
return `
favourite Foods
${foods.map(food => `- ${food}
`).join("")}
`
}
function age(birthYear) {
let calculatedAge = new Date().getFullYear() - birthYear;
if (calculatedAge == 1) {
return ' 1 year old'
} else if (calculatedAge == 0) {
return 'baby'
} else {
return `${calculatedAge} years old`
}
}
${petsData.map(petTemplate).join("")}
These ${pestData.length} pets were added
`